﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using ExamAnalysis.Core;
using ExamAnalysis.Model;
using ExamAnalysis.Core.Statistics;
using System.Xml;
using System.Xml.Xsl;

namespace ExamAnalysis.Reports
{
    public partial class MacroReport : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
            if (!X.IsAjaxRequest)
            {
                using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
                {
                    this.ClassStore.DataSource = db.ClassInfo
                        .Where(p => p.SchoolID == Config.CurrentSchoolID)
                        .ToList();
                    this.ClassStore.DataBind();
                }
                BindTargetList();
            }
        }

        [DirectMethod]
        protected void SubmitButton_Click(object sender, EventArgs e)
        {
            using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
            {
                if (TargetList.SelectedItem == null)
                {
                    X.Msg.Alert("通知", "请选择相应科目！").Show();
                }
                else
                {
                    List<MacroReportEntity> result;
                    switch (TargetType.Value.ToString())
                    {
                        case "所有科目":
                        case "科目组合":
                            //显示ExamGroup
                            int examGroupID = Convert.ToInt32(TargetList.SelectedItem.Value);
                            result = Report.ProcessMacroReport(
                                "ExamGroup", examGroupID, GetClassList(), GetStuCondition());
                            break;
                        case "单科科目":
                            //显示ExamInfo
                            int examID = Convert.ToInt32(TargetList.SelectedItem.Value);
                            result = Report.ProcessMacroReport(
                                "ExamInfo", examID, GetClassList(), GetStuCondition());
                            break;
                        case "科目分卷":
                            //显示ExamItemsGroup
                            int examItemGroupID = Convert.ToInt32(TargetList.SelectedItem.Value);
                            result = Report.ProcessMacroReport(
                                "ExamItemsGroup", examItemGroupID, GetClassList(), GetStuCondition());
                            break;
                        default:
                            result = new List<MacroReportEntity>();
                            break;
                    }
                    this.ReportStore.DataSource = result;
                    this.ReportStore.DataBind();
                    this.ChartStore.DataSource = result;
                    this.ChartStore.DataBind();
                }
            }
        }
        public string GetStuCondition()
        {
            switch (StudentType.Value.ToString())
            {
                case "全部":
                    return "";
                default:
                    return StudentType.Value.ToString();
            }
        }
        public List<int> GetClassList()
        {
            List<int> result = new List<int>();
            if (ClassList.Text.ToString() == "全部")
            {
                using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
                {
                    result = db.ClassInfo
                        .Where(p => p.SchoolID == Config.CurrentSchoolID)
                        .Select(p => p.ClassID)
                        .ToList();
                }
            }
            else
            {
                foreach (var item in (ClassGrid.GetSelectionModel() as RowSelectionModel).SelectedRows)
                {
                    result.Add(Convert.ToInt32(item.RecordID));
                }
            }
            return result;
        }

        [DirectMethod]
        public void TargetType_SelectedItemChanged(object sender, DirectEventArgs e)
        {
            BindTargetList();
        }

        protected void BindTargetList()
        {
            using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
            {
                switch (TargetType.Value.ToString())
                {
                    case "所有科目":
                        //显示ExamInGroup中的“所有科目”
                        var items0 = db.ExamGroup
                            .Where(p => p.SchoolID == Config.CurrentSchoolID
                                && p.GroupType == "所有科目")
                            .OrderByDescending(p=>p.ExamGroupID)
                            .ToList();
                        List<ValueTextEntity> store0 = new List<ValueTextEntity>();
                        foreach (var item0 in items0)
                        {
                            store0.Add(
                                new ValueTextEntity(
                                    item0.GroupName,
                                    item0.ExamGroupID.ToString(),
                                    "ExamGroup"
                                    ));
                        }
                        TargetStore.DataSource = store0;
                        TargetStore.DataBind();
                        break;
                    case "单科科目":
                        //ExamInfo
                        var items1 = db.ExamInfo
                            .Where(p => p.SchoolCalender.SchoolID == 2)
                            .OrderByDescending(p => p.CalendarID)
                            .ThenByDescending(p=>p.ExamID)
                            .ToList();
                        List<ValueTextEntity> store1 = new List<ValueTextEntity>();
                        foreach (var item1 in items1)
                        {
                            store1.Add(
                                new ValueTextEntity(
                                    item1.SchoolCalender.DisplayName + " - " +
                                    item1.ExamName,
                                    item1.ExamID.ToString(),
                                    "ExamInfo"
                                    ));
                        }
                        TargetStore.DataSource = store1;
                        TargetStore.DataBind();
                        break;
                    case "科目组合":
                        //显示ExamInGroup中的“科目组合”
                        var items2 = db.ExamGroup
                            .Where(p => p.SchoolID == Config.CurrentSchoolID
                                && p.GroupType == "科目组合")
                            .OrderByDescending(p => p.ExamGroupID)
                            .ToList();
                        List<ValueTextEntity> store2 = new List<ValueTextEntity>();
                        foreach (var item2 in items2)
                        {
                            store2.Add(
                                new ValueTextEntity(
                                    item2.GroupName,
                                    item2.ExamGroupID.ToString(),
                                    "ExamGroup"
                                    ));
                        }
                        TargetStore.DataSource = store2;
                        TargetStore.DataBind();
                        break;
                    case "科目分卷":
                        //显示ExamItemsGroup
                        var items3 = db.ExamItemsGroup
                            .Where(p => p.SchoolID == Config.CurrentSchoolID)
                            .OrderByDescending(p => p.ExamItemGroupID)
                            .ToList();
                        List<ValueTextEntity> store3 = new List<ValueTextEntity>();
                        foreach (var item3 in items3)
                        {
                            store3.Add(
                                new ValueTextEntity(
                                    item3.ExamInfo.ExamName + "-" + item3.GroupName,
                                    item3.ExamItemGroupID.ToString(),
                                    "ExamItemsGroup"
                                    ));
                        }
                        TargetStore.DataSource = store3;
                        TargetStore.DataBind();
                        break;
                }
            }
        }
        protected void Store_Submit(object sender, StoreSubmitDataEventArgs e)
        {
            string format = this.FormatType.Value.ToString();

            XmlNode xml = e.Xml;

            this.Response.Clear();

            switch (format)
            {
                case "xml":
                    string strXml = xml.OuterXml;
                    this.Response.AddHeader("Content-Disposition", "attachment; filename=submittedData.xml");
                    this.Response.AddHeader("Content-Length", strXml.Length.ToString());
                    this.Response.ContentType = "application/xml";
                    this.Response.Write(strXml);
                    break;

                case "xls":
                    this.Response.ContentType = "application/vnd.ms-excel";
                    this.Response.AddHeader("Content-Disposition", "attachment; filename=submittedData.xls");
                    XslCompiledTransform xtExcel = new XslCompiledTransform();
                    xtExcel.Load(Server.MapPath("~/Reports/Export/Excel.xsl"));
                    xtExcel.Transform(xml, null, Response.OutputStream);

                    break;

                case "csv":
                    this.Response.ContentType = "application/octet-stream";
                    this.Response.AddHeader("Content-Disposition", "attachment; filename=submittedData.csv");
                    XslCompiledTransform xtCsv = new XslCompiledTransform();
                    xtCsv.Load(Server.MapPath("~/Reports/Export/Csv.xsl"));
                    xtCsv.Transform(xml, null, Response.OutputStream);

                    break;
            }
            this.Response.End();
        }
    }
}